﻿using DevTeam;
using System.Collections.Generic;
using System.Data;

//DESTEX
namespace ConfExpert.Forms.ImportWizards
{
    public partial class Destex_LF_Tip2 : XITopCekiListesi
    {
        int YuklemeId;
        public Destex_LF_Tip2(int YuklemeId) :
            base(YuklemeId)
        {
            InitializeComponent();

            this.YuklemeId = YuklemeId;
            this.Init("Destex LF Tip2 Top Listesi", true);
            this.FileDone += DestexTip1_FileDone;
        }

        void DestexTip1_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                DataRowCollection rows = dt.Rows;
                DataColumnCollection cols = dt.Columns;

                int orderLineStart = -1;
                int orderLineEnd = -1;

                string orderNo = "";
                string construction = "";
                string patternNo = "";


                for (int i = 0; i < rows.Count; i++)
                {
                    DataRow dr = rows[i];
                    if (dr[0].ToStr().ToUpper() == "DO NO. :")
                        orderNo = dr[2].ToStr();

                    else if (dr[0].ToStr().ToUpper() == "CONSTRUCTION")
                        construction = dr[2].ToStr();

                    else if (dr[0].ToStr().ToUpper() == "PATTERN NO.:")
                    {
                        patternNo = dr[2].ToStr();
                        if (dr[3].ToStr() != string.Empty)
                            patternNo = dr[3].ToStr();
                        if (dr[4].ToStr() != string.Empty)
                            patternNo = dr[4].ToStr();

                        if (patternNo.Contains(" "))
                        {
                            string[] parts = patternNo.Split(" ".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
                            if (parts.Length == 2 && parts[1].Length>4)
                                patternNo = parts[1];

                            //switch (patternNo)
                            //{
                            //    case "ESP.PX843D/00": patternNo = "ESP.PX834D/00.NON"; break;
                            //    case "100860903/MAVI": patternNo = "100860903/MAVİ"; break;
                            //}
                        }
                    }
                    else if (dr[0].ToStr().ToUpper().Contains("ROLL"))
                        orderLineStart = i + 2;

                    else if (
                        orderLineStart > -1 &&
                        orderLineEnd == -1 &&
                        i > orderLineStart &&

                        dr[0].ToStr() == "" &&
                        dr[1].ToStr() == ""
                        )
                        orderLineEnd = i - 1;
                }

                patternNo = patternNo.Trim();

                Dictionary<string, int> indexes = new Dictionary<string, int>();

                for (int i = 1; i < 3; i++)
                {
                    DataRow dr0 = dt.Rows[orderLineStart - i];
                    for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                    {
                        switch (dr0[colIndex].ToString())
                        {
                            case "ROLL NO.":
                            case "ROLL": indexes.Add("VendorRollId", colIndex); break;
                            case "COLOUR": indexes.Add("LotNo", colIndex); break;
                            //case "TICKET":
                            //    if (!indexes.ContainsKey("WidthActual"))
                            //        indexes.Add("WidthActual", colIndex); break; //DİKKAT: actual vs. ticket!
                            //case "ACTUAL":
                            //    if (!indexes.ContainsKey("WidthUsable"))
                            //        indexes.Add("WidthUsable", colIndex);
                            //    else if (!indexes.ContainsKey("Qty"))
                            //        indexes.Add("Qty", colIndex);
                            //    break;

                            case "CUTTABLE":
                                if (!indexes.ContainsKey("WidthUsable"))
                                    indexes.Add("WidthUsable", colIndex);
                                break;

                            case "ACTUAL":
                                if (!indexes.ContainsKey("WidthUsable"))
                                    indexes.Add("WidthUsable", colIndex);
                                else if (!indexes.ContainsKey("Qty"))
                                    indexes.Add("Qty", colIndex);
                                break;

                        }
                    }
                }



                for (int i = orderLineStart; i < orderLineEnd + 1; i++)
                {
                    DataRow dr = rows[i];
                    if (Article.Select("Article='" + patternNo + "'").Length == 0)
                    {
                        DataRow drArticle = Article.NewRow();
                        drArticle["Article"] = patternNo;
                        drArticle["VendorConstruction"] = construction;
                        Article.Rows.Add(drArticle);
                    }

                    DataRow drRoll = Roll.NewRow();
                    if (indexes.ContainsKey("VendorRollId"))
                        drRoll["VendorRollId"] = dr[indexes["VendorRollId"]];
                    if (indexes.ContainsKey("LotNo"))
                        drRoll["LotNo"] = dr[indexes["LotNo"]];
                    if (indexes.ContainsKey("WidthActual"))
                        drRoll["WidthActual"] = dr[indexes["WidthActual"]];
                    if (indexes.ContainsKey("WidthUsable"))
                        drRoll["WidthUsable"] = dr[indexes["WidthUsable"]];
                    drRoll["Article"] = patternNo;
                    drRoll["Qty"] = dr[indexes["Qty"]];
                    Roll.Rows.Add(drRoll);
                }
            }

            //TODO: peyderpey gelişte, yeni yapıda, ayrımın KALAN kısmını net olarak bilmek gerek... KST'de yeni satır açılıyor ama bunda öyle değil!

        }

    }
}